<?php
/**
 * 备货sku日志
 * (model)
 */
namespace App\Models\Stocking;

use App\Models\Basics\SysArea;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;
class StockingSkuLog extends Model
{
    protected $table = 's_stocking_sku_log';

    protected $guarded = [];

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function skuClassify(){
        return $this->hasOneThrough(
            ProductClassify::class,
            ProductSku::class,
            'id',//中间表
            'id',//目标表
            'psku_id',//当前模型
            'classify_id'//中间表
        );
    }

    /**
     * 日志
     * @param $stockup
     */
    public static function _addStockingSkuLog($stockup){
        $log = [];
        $log['area_id'] = $stockup->area_id;
        $log['psku_id'] = $stockup->psku_id;
        $log['ratio'] = $stockup->ratio->ratio ?? 0;
        $log['fba_instant_num'] = $stockup->fba_instant_num;
        $log['fba_transit_num'] = $stockup->fba_transit_num;
        $log['cn_instant_num'] = $stockup->cn_instant_num;
        $log['cn_transit_num'] = $stockup->cn_transit_num;
        $log['os_instant_num'] = $stockup->os_instant_num;
        $log['os_transit_num'] = $stockup->os_transit_num;
        $log['apply_num'] = $stockup->confirm_num;
        $log['approval_num'] = $stockup->approval_num;
        $log['apply_remark'] = $stockup->apply_remark;
        $log['approval_remark'] = $stockup->approval_remark;
        $log['seller_id'] = $stockup->seller_id;
        $log['approval_date'] = date('Y-m-d');
        StockingSkuLog::query()->create($log);
    }
}
